package com.atguigu.auth.controller;


import com.atguigu.auth.service.SysPostService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysPost;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysPostQueryVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author rich
 * @since 2023-09-24
 */
@Api(tags = "岗位管理接口")
@RestController
@RequestMapping("/admin/system/sysPost")
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;


    @ApiOperation("查询所有岗位")
    @GetMapping("findAll")
    public Result<List<SysPost>> findAll() {
        List<SysPost> postList = sysPostService.list();
        return Result.ok(postList);
    }

    //条件分页查询
    //SysPostQueryVo 条件对象 code name status
    @ApiOperation("条件分页查询")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    @GetMapping("{page}/{limit}")
    public Result pageQueryPost(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysPostQueryVo sysPostQueryVo) {
        //调用service的方法实现
        //1 创建Page对象，传递分页相关参数
        //page 当前页  limit 每页显示记录数
        Page<SysPost> pageParam = new Page<>(page,limit);

        //2 封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        String postCode = sysPostQueryVo.getPostCode();
        Boolean status = sysPostQueryVo.getStatus();
        String postName = sysPostQueryVo.getName();
        if(!StringUtils.isEmpty(postName)) {
            //封装 like模糊查询
            wrapper.like(SysPost::getName,postName);
        }
        if(!StringUtils.isEmpty(status)) {
            wrapper.eq(SysPost::getStatus,status);
        }
        if(!StringUtils.isEmpty(postCode)) {
            wrapper.eq(SysPost::getPostCode,postCode);
        }
        //3 调用方法实现
        IPage<SysPost> pageModel = sysPostService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    //添加岗位
    @ApiOperation("添加岗位")
    @PreAuthorize("hasAuthority('bnt.sysPost.add')")
    @PostMapping("/save")
    public Result save(@RequestBody SysPost sysPost){
        boolean isSave = sysPostService.save(sysPost);
        if(isSave) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //修改岗位 -根据id查询
    @ApiOperation("根据id查询")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Integer id){
        SysPost sysPost = sysPostService.getById(id);
        return Result.ok(sysPost);
    }

    //修改岗位 - 最终修改
    @ApiOperation("修改岗位")
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @PutMapping("/update")
    public Result update(@RequestBody SysPost sysPost){
        boolean isSave = sysPostService.updateById(sysPost);
        if(isSave) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Integer id){
        boolean isDelete = sysPostService.removeById(id);
        if(isDelete){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //批量删除
    //前端数组 [1,2,3]
    @ApiOperation("批量删除")
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean isDelete = sysPostService.removeByIds(idList);
        if(isDelete){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysPostService.updateStatus(id, status);
        return Result.ok();
    }
}

